package com.one.controller;

import com.one.bean.User;
import com.one.constant.RequestReturnConstant;
import com.one.service.LoginService;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.WechatUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @Author ShiWen
 * @date 2022/1/14 12:13
 * @Version 1.0
 * @Description TODO 登录操作入口
 */
@Api(tags = "C.登录操作")
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    /**
     *  用户密码登录
     */
    @ApiOperation(value = "使用手机号或密码登录")
    @PostMapping("/loginByPwd")
    public RequestReturn<Map<String,Object>> pwdLogin(String phone,String password){
        if (StringUtils.isEmpty(phone)||StringUtils.isEmpty(password)){
            return RequestReturnFactory.fail(RequestReturnConstant.PHONE_PASSWORD_FAIL_CODE,
                    RequestReturnConstant.PHONE_PASSWORD_FAIL_MSG);
        }
        // 进行登录
        return loginService.phoneDoLogin(phone, password);
    }
    /**
     *  手机验证码登录
     */
    @ApiOperation(value = "使用手机验证码登录")
    @PostMapping("/phoneAuth")
    public RequestReturn<Map<String,Object>> phoneAuth(@RequestParam String phone,@RequestParam String auth){
        return loginService.phoneAuth(phone, auth);
    }

    /**
     *  生成短信验证码
     */
    @ApiOperation(value = "生成短信验证码")
    @GetMapping("/sendCode")
    public RequestReturn<Map<String,Object>> sendAuthCode(@RequestParam("phone") String phone){
        return loginService.sendAuthCode(phone);
    }

    // 查看验证码
    @ApiOperation(value = "查看验证码")
    @GetMapping("/login/queryCode")
    public RequestReturn<Map<String,Object>> selectAuthCode(@RequestParam("phone") String phone){
        return loginService.selectAuthCode(phone);
    }
    /**
     *  微信注册绑定手机号
     */
    @ApiOperation(value = "微信注册绑定手机号")
    @GetMapping("/login/bindingPhone")
    public RequestReturn<Map<String,Object>> bindingPhone(@RequestParam("wechatUser") String wechatUser,
                                       @RequestParam("phone") String phone,
                                       @RequestParam("auth") String auth){
        return loginService.bindingPhone(wechatUser, phone, auth);
    }

    /**
     *  请求微信获取请求微信公众测试平台，手动授权，获取code的连接（完整url）
     */
    @ApiOperation(value = "获取请求微信公众测试平台，手动授权，获取code的连接")
    @GetMapping("/codeUrl")
    public RequestReturn<Map<String,Object>> wechatAuthCodeUrl(){
        return loginService.wechatAuthCodeUrl();
    }


}
